package com.hyh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.ad.common.utils.SecurityUtils;
import com.hyh.admin.domain.resources.Resources;
import com.hyh.admin.domain.resources.ResourcesCollect;
import com.hyh.admin.mapper.resources.ResourcesCollectMapper;
import com.hyh.admin.service.IResourcesCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/*
 *  资源收藏类-实现类
 */
@Service
public class ResourcesCollectServiceImpl implements IResourcesCollectService {

    @Autowired
    private ResourcesCollectMapper resourcesCollectMapper;

    @Override
    public List<Resources> selectResourcesCollect(Long userId) {
        return resourcesCollectMapper.selectResourcesCollect(userId);
    }

    @Override
    public int deleteResourcesCollect(Long userId, Long resourceId) {
        LambdaQueryWrapper<ResourcesCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourcesCollect::getUserId, userId);
        queryWrapper.eq(ResourcesCollect::getResourcesId, resourceId);
        return resourcesCollectMapper.delete(queryWrapper);
    }

    @Override
    public int insertResourcesCollect(Long userId, Long resourceId) {
        ResourcesCollect resourcesCollect = new ResourcesCollect();
        resourcesCollect.setUserId(userId);
        resourcesCollect.setResourcesId(resourceId);
        LambdaQueryWrapper<ResourcesCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourcesCollect::getUserId, userId);
        queryWrapper.eq(ResourcesCollect::getResourcesId, resourceId);
        ResourcesCollect resourcesCollect1 = resourcesCollectMapper.selectOne(queryWrapper);
        if (resourcesCollect1 != null) {
           resourcesCollectMapper.updateById(resourcesCollect1);
        }
        return resourcesCollectMapper.insert(resourcesCollect);
    }

    @Override
    public int deleteResourcesCollectBatch(Long userId, Long[] ids) {
        LambdaQueryWrapper<ResourcesCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourcesCollect::getUserId, SecurityUtils.getLoginUser().getUser().getId());
        queryWrapper.in(ResourcesCollect::getResourcesId, ids);
        return resourcesCollectMapper.delete(queryWrapper);
    }
}
